/*
placeholder class...intended to be used as a surrogate for various Enyaras
*/

class KObject extends NetKActor;

var Actor Seeking;
var vector InitialDir;
var vector Dir;
var float Speed;
var float DefaultSpeed;

var bool bTelekAffected; /* wheter or not the obj is currently being affected by
                         telekenesis */
var float AffectedCheckTime, AffectedCheckFrequency;
var float LastDamagedTimeStamp;
var vector TargetPosition; //where this object wants to move to; aka the source of the telek dmg (aka the player)


function Tick(float dt){
    local vector ForceDir;
    local float AffectedDuration; //length of time between last telek dmg hit
    local float ForceMag;
    local float VelMag;

    AffectedDuration = Level.TimeSeconds - LastDamagedTimeStamp;

    if (bTelekAffected){   //if object is currently affected by telek...

        if(AffectedDuration > AffectedCheckFrequency){
          //too much time has passed since last telek damagetype hit
          //so set the physics back to karma and consider the object
          //no longer affected by telek

          bTelekAffected = false;

          SetPhysics(PHYS_Karma);
          KWake();
          //zero out kinematic properties
          Acceleration = vect(0,0,0);
          Velocity = vect(0,0,0);
          log("no longer affected by telek");
        }
        else{
          //if still affected by telek, update physics/position info
            SetPhysics(PHYS_Projectile);
        	Velocity =  DefaultSpeed * Normal(Dir * 0.5 * DefaultSpeed + Velocity);
      		ForceDir = Normal(Seeking.Location - Location);

             if ( InitialDir == vect(0,0,0) )
                InitialDir = ForceDir;



        		if( (ForceDir dot InitialDir) > 0 )
        		{
        			VelMag = VSize(Velocity);
       				ForceDir = Normal(ForceDir * 0.5 * VelMag + Velocity);
        			Velocity =  VelMag * ForceDir;
        			Acceleration += 5 * ForceDir;
        			//log("forcedir: " @ ForceDir @ "Velocity: " @ Velocity @ "accel: " @ Acceleration);
        		}
        		// Update rocket so it faces in the direction its going.
        		SetRotation(rotator(Velocity));
          }
    }

   /*
    local Pawn Other;
    local Vector StartTrace;
    local Rotator Aim;
    local float BestDist, BestAim;

    if (Instigator == None || Instigator.Weapon != self)
        return;

	if ( Role < ROLE_Authority )
		return;

    if ( !Instigator.IsHumanControlled() )
        return;

    if (Level.TimeSeconds > SeekCheckTime)
    {
        if (bBreakLock)
        {
            bBreakLock = false;
            bLockedOn = false;
            SeekTarget = None;
        }

        StartTrace = Instigator.Location + Instigator.EyePosition();
        Aim = Instigator.GetViewRotation();

        BestAim = LockAim;
        Other = Instigator.Controller.PickTarget(BestAim, BestDist, Vector(Aim), StartTrace, SeekRange);

        if ( CanLockOnTo(Other) )
        {
            if (Other == SeekTarget)
            {
                LockTime += SeekCheckFreq;
                if (!bLockedOn && LockTime >= LockRequiredTime)
                {
                    bLockedOn = true;
                    PlayerController(Instigator.Controller).ClientPlaySound(Sound'WeaponSounds.LockOn');
                 }
            }
            else
            {
                SeekTarget = Other;
                LockTime = 0.0;
            }
            UnLockTime = 0.0;
        }
        else
        {
            if (SeekTarget != None)
            {
                UnLockTime += SeekCheckFreq;
                if (UnLockTime >= UnLockRequiredTime)
                {
                    SeekTarget = None;
                    if (bLockedOn)
                    {
                        bLockedOn = false;
                        PlayerController(Instigator.Controller).ClientPlaySound(Sound'WeaponSounds.SeekLost');
                    }
                }
            }
            else
                 bLockedOn = false;
         }

        SeekCheckTime = Level.TimeSeconds + SeekCheckFreq;
    }
    */
}
/*
simulated function Timer()
{
    local vector ForceDir, CurlDir;
    local float ForceMag;
    local int i;
    local float VelMag;

	Acceleration = vect(0,0,0);

	Velocity =  default.Speed * Normal(Dir * 0.5 * default.Speed + Velocity);

    // Do normal guidance to target.
		ForceDir = Normal(Seeking.Location - Location);


     if ( InitialDir == vect(0,0,0) )
        InitialDir = ForceDir;

	//	log("Seeking " @ self.Physics @ ", forcedir " @ ForceDir @ ", InitialDir " @ InitialDir @ ", Velocity " @ Velocity);


		if( (ForceDir Dot InitialDir) > 0 )
		{
			VelMag = VSize(Velocity);

			// track vehicles better
			if ( Seeking.Physics == PHYS_Karma )
				ForceDir = Normal(ForceDir * 0.8 * VelMag + Velocity);
			else
				ForceDir = Normal(ForceDir * 0.5 * VelMag + Velocity);
			Velocity =  VelMag * ForceDir;
			Acceleration += 5 * ForceDir;
		}
		// Update rocket so it faces in the direction its going.
		SetRotation(rotator(Velocity));

    if (Location.X == Seeking.Location.X){
       log("GREP");
       SetPhysics(PHYS_Karma);
       SetTimer(0, false);
    }

}
*/

function


event TakeDamage ( int Damage, Pawn EventInstigator, vector HitLocation, vector Momentum, class<DamageType> DamageType ){
    local vector ApplyImpulse;
    local float tmpDuration;


    // super.TakeDamage(Damage, EventInstigator, HitLocation, Momentum, DamageType);

    if(damageType == class'bblock.DamTypeTelekShot' || damageType == class'bblock.DamTypeLock' ){
         tmpDuration = Level.TimeSeconds - LastDamagedTimeStamp;
    //    log("game time: "@Level.TimeSeconds@" last dmg: "@LastDamagedTimeStamp@"time difference: "@tmpDuration);


        EventInstigator.ClientMessage("Hit " @ self.name @ " with damage type " @ DamageType);
        LastDamagedTimeStamp = Level.TimeSeconds;
        //now affected by telek
        if (!bTelekAffected) log("affected by telek");
        bTelekAffected = true;




        Speed=DefaultSpeed;

        Dir=(EventInstigator.Location - Location);
        Seeking = EventInstigator;
        //Velocity=(Speed*Dir);
        //
       // SetTimer(0.1, true);
        //SetPhysics(PHYS_Karma);

    }
    if((damageType.default.KDamageImpulse > 0) && !bTelekAffected)
    {
	    if(VSize(momentum) < 0.001)
	    {
           return;
        }
	    ApplyImpulse = Normal(momentum) * damageType.default.KDamageImpulse;
	    //DEBUG
	    //log("********************") ;
	    //log(ApplyImpulse) ;
	    KAddImpulse(ApplyImpulse, hitlocation) ;
    }
}



defaultproperties{
    //Speed=1350.000000
     bCollideActors=True
     bCollideWorld=True
     bTelekAffected=false
     AffectedCheckFrequency=0.2
     DefaultSpeed=3000
}
